package com.org.controller;

import com.org.entity.Users;
import com.org.entity.bo.UserBO;
import com.org.service.UserService;
import com.org.utils.CookieUtils;
import com.org.utils.JSONResult;
import com.org.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "注册登录",tags = {"用于注册登录的相关接口"})
@RestController
@RequestMapping("passport")
public class PassportController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET")
    @GetMapping("/usernameIsExist")
    public JSONResult usernameIsExist(@RequestParam String username){

        //判断用户名不能为空
        if (StringUtils.isBlank(username)){
            return JSONResult.errorMsg("用户名不能为空");
        }

        //查找注册的用户名是否存在
        boolean isExist=userService.queryUserNameIsExist(username);

        if (isExist){
            return JSONResult.errorMsg("用户名已存在");
        }

        //用户名无重复
        return JSONResult.ok();
    }

    @ApiOperation(value = "创建用户",httpMethod = "POST",notes = "用户注册")
    @PostMapping("/regist")
    public JSONResult regist(@RequestParam UserBO userBO, HttpServletRequest request, HttpServletResponse response){
        String username=userBO.getUsername();
        String password=userBO.getPassword();
        String confirmPassword=userBO.getConfirmPassword();

        if (StringUtils.isBlank(username)||StringUtils.isBlank(password)||StringUtils.isBlank(confirmPassword)){
            return JSONResult.errorMsg("用户名或密码不能为空");
        }

        boolean isExist=userService.queryUserNameIsExist(username);
        if (isExist){
            return JSONResult.errorMsg("用户民已经存在");
        }

        if (password.length()<6){
            return JSONResult.errorMsg("密码长度不能小于6");
        }

        if (!password.equals(confirmPassword)){
            return JSONResult.errorMsg("两次密码不一致");
        }

        Users userResult=userService.createUser(userBO);
        userResult=setNullProperty(userResult);

        return JSONResult.ok(userResult);

    }

    private Users setNullProperty(Users userResult){
        userResult.setPassword(null);
        userResult.setRealname(null);
        userResult.setBirthday(null);
        userResult.setCreatedTime(null);
        userResult.setMobile(null);
        userResult.setEmail(null);

        return userResult;
    }

    @ApiOperation(value = "用户登录",notes = "用户登录",httpMethod = "POST")
    @PostMapping("/login")
    public JSONResult login(@RequestBody UserBO userBO, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username=userBO.getUsername();
        String password=userBO.getPassword();

        if (StringUtils.isBlank(username)||StringUtils.isBlank(password)){
            return JSONResult.errorMsg("用户名或密码不能为空");
        }

        Users userResult=userService.queryUserForLogin(username,
                MD5Utils.getMDStr(password));

        if (userResult==null){
            return JSONResult.errorMsg("用户名或密码不正确");
        }

        userResult=setNullProperty(userResult);

        return JSONResult.ok(userResult);

    }

    @ApiOperation(value = "用户退出登录",notes = "用户退出登录",httpMethod = "POST")
    @PostMapping("/logout")
    public JSONResult logout(@RequestBody String userId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        CookieUtils.deleteCookie(request,response,"user");
        return JSONResult.ok();

    }



}
